package com.dadi.lminglearn;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    private ThreadLocal<String> mThreadLocal = new ThreadLocal<String>(){
        @Override
        protected String initialValue() {
            return "Init ThreadlLocal";
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        Log.e("ThreadLocal","thread " + Thread.currentThread() + " " + mThreadLocal.get());
                        mThreadLocal.set("Test1");
                        SystemClock.sleep(2000);
                        Log.e("ThreadLocal", "thread " + Thread.currentThread() + " " + mThreadLocal.get());
                    }
                }
        ).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e("ThreadLocal","thread " + Thread.currentThread() + " " + mThreadLocal.get());
                mThreadLocal.set("Test2");
                Log.e("ThreadLocal","thread " + Thread.currentThread() + " " + mThreadLocal.get());
            }
        }).start();
    }
}
